<template>
  <div class="login">
    <div class="login-form">
      <h3 class="title" style="text-align:center">税银管理系统</h3>
      <el-form ref="loginRef" :model="loginForm" :rules="loginRules" label-width="90px">
        <el-form-item prop="userName" label="账号">
          <el-input v-model="loginForm.userName" type="text" size="large" auto-complete="off" placeholder="请输入账号">
            <template #prefix><svg-icon icon-class="user" class="el-input__icon input-icon" /></template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password" label="密码">
          <el-input v-model="loginForm.password" type="password" size="large" auto-complete="off" placeholder="请输入密码"
            show-password @keyup.enter="handleLogin">
            <template #prefix><svg-icon icon-class="password" class="el-input__icon input-icon" /></template>
          </el-input>
        </el-form-item>
        <el-checkbox v-model="loginForm.rememberMe" style="margin: 0px 0px 10px 90px">记住密码</el-checkbox>
      </el-form>
      <div class="text-center pb20">
        <el-button :loading="loading" size="large" type="primary" style="width: 90%;background-color:#326FEA"
          @click.prevent="handleLogin">
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
      </div>
    </div>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>为了更好的网站体验，建议您使用谷歌、火狐、360极速模式等高版本的浏览器</span>
    </div>

  </div>
</template>

<script setup>
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";

const store = useStore();
const router = useRouter();
const { proxy } = getCurrentInstance();

const loginForm = ref({
  userName: "",
  password: "",
});

const loginRules = {
  userName: [{ required: true, trigger: "blur", message: "请输入您的账号" }],
  password: [{ required: true, trigger: "blur", message: "请输入您的密码" }],
};
const loading = ref(false);
const redirect = ref(undefined);

function handleLogin() {
  proxy.$refs.loginRef.validate((valid) => {
    if (valid) {
      loading.value = true;
      Cookies.set("tax-silver-userName", loginForm.value.userName, { expires: 30 });
      // 勾选了需要记住密码设置在cookie中设置记住用户明和名命
      if (loginForm.value.rememberMe) {
        Cookies.set("tax-silver-password", encrypt(loginForm.value.password), { expires: 30 });
        Cookies.set("tax-silver-rememberMe", loginForm.value.rememberMe, { expires: 30 });
      } else {
        // 否则移除
        // Cookies.remove("tax-silver-userName");
        Cookies.remove("tax-silver-password");
        Cookies.remove("tax-silver-rememberMe");
      }
      const reqForm = {
        userName: loginForm.value.userName.trim(),
        password: loginForm.value.password,
      }
      store.dispatch("Login", reqForm).then((res) => {
        router.push({ path: redirect.value || "/" });
      }).finally(() => loading.value = false)
    }
  });
}


function getCookie() {
  const userName = Cookies.get("tax-silver-userName");
  const password = Cookies.get("tax-silver-password");
  const rememberMe = Cookies.get("tax-silver-rememberMe");
  loginForm.value = {
    userName: userName === undefined ? loginForm.value.userName : userName,
    password: password === undefined ? loginForm.value.password : decrypt(password),
    rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
  };
}
getCookie();

</script>

<style lang="scss" scoped>
.login {
  position: relative;
  display: flex;
  height: 100vh;
  width: 100vw;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: url('../assets/images/bg_new.jpg') no-repeat;
  background-size: 100% 100%;
  // background-repeat: no-repeat;
  // background-size: cover;

  .login-form {
    width: 450px;
    background-color: #fff;
    box-shadow: 3px 8px 15px 13px rgba(255, 255, 255, 0.4);
    border-radius: 10px;

    .title {
      font-size: 24px;
      color: #fff;
      padding: 20px;
      margin: 0 0 20px 0;
      background-color: #4579b2;
      border-top-left-radius: 10px;
      border-top-right-radius: 10px;
    }

    .el-form {
      padding-right: 20px;
    }

    .login-code {
      width: 30%;
      display: flex;
      align-items: center;
      margin-left: 20px;

      img {
        width: 100%;
      }
    }
  }
}

.el-login-footer {
  position: absolute;
  color: #fff;
  bottom: 5%;
}
</style>
<style lang="scss">
.login-form .el-input__prefix {
  align-items: center;
}
</style>